package com.neusoft.neu24g3.myhis2.demos.util;

import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *  自定义SpringNVC的类型转换器
 *  1、实现Converter <String, Xxxx>  目标类型
 *  2、类名无所谓
 *  3、@Component 在Spring容器中注册
 *  4、convert 是实现String - - > Xxxx转化的具体的方法
 *
 * @author Administrator
 * @version 2024/05/27 16:52
 **/
@Component
public class MyDateStringConverter  implements Converter<String, Date> {
    private SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * Convert the source object of type {@code S} to target type {@code T}.
     *
     * @param source the source object to convert, which must be an instance of {@code S} (never {@code null})
     * @return the converted object, which must be an instance of {@code T} (potentially {@code null})
     * @throws IllegalArgumentException if the source cannot be converted to the desired target type
     */
    @Override
    public Date convert(String source) {
        try {
            return format.parse( source);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return  null;
    }
}